#!/bin/bash

# 用户名
MYSQL_USER='zabbix'
# 密码
MYSQL_PWD='Thk~@#8695gHf'
# 主机地址/IP
MYSQL_HOST='127.0.0.1'
# 端口
MYSQL_PORT='3307'
# 数据连接
MYSQL_CONN="mysql -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT} -e"
# 参数是否正确
if [ $# -ne "1" ];then 
    echo "arg error!" 
fi 
######################################### 获取数据  ###################################################


#Queries per second avg:历史以来平均每秒查询数                             
#       mysqladmin proc stat -uzabbix -p'Thk~@#8695gHf'  2>/dev/null | grep Uptime | awk '{print $22}'  
#Sleep：当前Sleep线程数量
#       mysqladmin  -uzabbix -p'Thk~@#8695gHf'  processlist 2>/dev/null| grep Sleep | wc -l
#Query：当前Query线程数量
#       mysqladmin  -uzabbix -p'Thk~@#8695gHf'  processlist 2>/dev/null| grep Query |wc -l
#Locked ：当前Locked线程数量（正等待表锁的数量）
#       mysqladmin  -uzabbix -p'Thk~@#8695gHf'  processlist 2>/dev/null| grep Locked |wc -l

#当前连接数：(通用)
#     mysql -uzabbix -p'Thk~@#8695gHf'  -e  "show status like '%connect%';"  2> /dev/null  | grep Threads_connected | awk '{print $2}'
#当前并发数：(通用)
#     mysql -uzabbix -p'Thk~@#8695gHf'  -e  "show status like 'Threads%';"  2> /dev/null  | grep Threads_running | awk '{print $2}'
#当前缓冲池中的线程个数：(通用)
#     mysql -uzabbix -p'Thk~@#8695gHf'  -e  "show status like 'Threads%';"  2> /dev/null  | grep Threads_cached | awk '{print $2}'
#历史以来最大的并发数：（通用）
#     mysql -uzabbix -p'Thk~@#8695gHf'  -e  "show status like '%connect%';"  2> /dev/null  | grep -w Max_used_connections | awk '{print $2}'	



case $1 in 
    Queries) 
        result=`mysqladmin proc stat -uzabbix -p'Thk~@#8695gHf'  2>/dev/null | grep Uptime | awk '{print $22}'` 
	    echo $result
        ;; 
    Sleep) 
        result=`mysqladmin  -uzabbix -p'Thk~@#8695gHf'  processlist 2>/dev/null| grep Sleep | wc -l`      
	    echo $result 
        ;; 
    Query) 
        result=`mysqladmin  -uzabbix -p'Thk~@#8695gHf'  processlist 2>/dev/null| grep Query |wc -l` 
	    echo $result
                ;; 
    Locked) 
        result=`mysqladmin  -uzabbix -p'Thk~@#8695gHf'  processlist 2>/dev/null| grep Locked |wc -l` 
	    echo $result
                ;; 
    Threads_connected) 
        result=`${MYSQL_CONN} "show status like '%connect%';"  2> /dev/null  | grep Threads_connected | awk '{print $2}'` 
        echo $result
                ;; 
    Threads_running) 
        result=`${MYSQL_CONN} "show status like 'Threads%';"  2> /dev/null  | grep Threads_running | awk '{print $2}'`   
        echo $result              
                ;; 
    Threads_cached) 
        result=`${MYSQL_CONN} "show status like 'Threads%';"  2> /dev/null  | grep Threads_cached | awk '{print $2}'`  
        echo $result              
                ;; 
    Max_used_connections) 
        result=`${MYSQL_CONN} "show status like '%connect%';"  2> /dev/null  | grep -w Max_used_connections | awk '{print $2}'` 
        echo $result              
                ;; 
        *) 
        echo "Usage:$0(|Queries|Sleep|Query|Locked|Threads_connected|Threads_running|Threads_cached|Threads_cached2|Max_used_connections|)" 
        ;; 
esac
